import router from './router'
import store from './store'
NProgress.configure({ showSpinner: false }) // NProgress Configuration
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { asyncRoutes } from './router/index'
// import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
const whitelist = ['/login','/404']
router.beforeEach( async (to, from, next) => {
    NProgress.start()
    const token = store.state.user.token
    if(token) {
      // 去登录页吗
      if(to.path === '/login') {
        next('/')
        NProgress.done()
      } else {
        const userInfo = store.state.user.userInfo
        if(!userInfo.userId) {
          // 获取权限信息
          const res = await store.dispatch('user/getUserProfile') 
          
          // 根据用户的权限信息, 从asyncRoutes过滤出一部分
          console.log('路由守卫', asyncRoutes, res.data.roles.menus)
          const menus = res.data.roles.menus
          const filterRoutes = asyncRoutes.filter(item => {
            return menus.includes(item.children[0].name)
          })
          // 动态生成可以访问的动态路由(根据权限信息动态添加路由)
          // (1)可以让我们在地址栏中访问页面
          filterRoutes.push({ path: '*', redirect: '/404', hidden: true })
          router.addRoutes(filterRoutes)
          // (2)把路由信息保存到vuex
          store.commit('menu/updateMenuList', filterRoutes)
          next({
            ...to,
            replace: true
          })
        } else {
          next()
        }
        
      }
    } else {
        // 是否访问白名单中的页面
        if(whitelist.includes(to.path)) {
        next()
      } else {
        // 没有token, 去的不是login
        next('/login')
        NProgress.done()
      }
    }
})

router.beforeEach( async (to, from, next) => {
  console.log('守卫2', to.meta.title)
  document.title = getPageTitle(to.meta.title)
  next()
})

router.afterEach((to, from) => {
  NProgress.done()
})







// const whiteList = ['/login'] // no redirect whitelist

// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
